package cn.edu.ncut.cs.springboot.petmanagementsystem.controller;

import cn.edu.ncut.cs.springboot.petmanagementsystem.common.Result;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.Coupon;
import cn.edu.ncut.cs.springboot.petmanagementsystem.pojo.UserCoupon;
import cn.edu.ncut.cs.springboot.petmanagementsystem.service.CouponService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/coupon")
@Tag(name = "优惠券管理")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @Operation(summary = "获取用户优惠券列表")
    @GetMapping("/my-list")
    public Result<List<UserCoupon>> getMyCoupons() {
        try {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(auth.getCredentials().toString());
            List<UserCoupon> coupons = couponService.getUserCoupons(userId);
            return Result.success(coupons);
        } catch (Exception e) {
            log.error("获取用户优惠券失败", e);
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "领取优惠券")
    @PostMapping("/receive/{id}")
    public Result<Void> receiveCoupon(
            @Parameter(description = "优惠券ID") @PathVariable Long id) {
        try {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(auth.getCredentials().toString());
            couponService.receiveCoupon(userId, id);
            return Result.success("领取成功");
        } catch (Exception e) {
            log.error("领取优惠券失败", e);
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "获取可用优惠券")
    @GetMapping("/available")
    public Result<List<Coupon>> getAvailableCoupons() {
        try {
            List<Coupon> coupons = couponService.getAvailableCoupons();
            return Result.success(coupons);
        } catch (Exception e) {
            log.error("获取可用优惠券失败", e);
            return Result.error(e.getMessage());
        }
    }

    @Operation(summary = "检查优惠券可用性")
    @GetMapping("/check/{id}")
    public Result<Boolean> checkCoupon(
            @Parameter(description = "优惠券ID") @PathVariable Long id) {
        try {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            Long userId = Long.parseLong(auth.getCredentials().toString());
            boolean available = couponService.checkCouponAvailability(id, userId);
            return Result.success(available);
        } catch (Exception e) {
            log.error("检查优惠券失败", e);
            return Result.error(e.getMessage());
        }
    }
}
